package com.ljd.hosp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ljd.hosp.mapper.HospitalSetMapper;
import com.ljd.hosp.service.HospitalSetService;
import com.ljd.yygh.exception.YyghException;
import com.ljd.yygh.model.hosp.HospitalSet;
import com.ljd.yygh.result.ResultCodeEnum;
import com.ljd.yygh.vo.hosp.HospitalSetQueryVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.swing.plaf.basic.BasicScrollPaneUI;

@Service
public class HospitalSetServiceImpl extends ServiceImpl<HospitalSetMapper, HospitalSet> implements HospitalSetService {

    @Override
    public Page<HospitalSet> findPageHospitalSet(Long current, Long limit, HospitalSetQueryVo vo) {
        Page<HospitalSet> page = new Page<>(current, limit);
        LambdaQueryWrapper<HospitalSet> wrapper = new LambdaQueryWrapper<>();
        if(vo!=null) {
            String hoscode = vo.getHoscode();
            String hosname = vo.getHosname();
            if(StringUtils.isNotEmpty(hoscode)) {
                wrapper.eq(HospitalSet::getHoscode,hoscode);
            }
            if(StringUtils.isNotEmpty(hosname)) {
                wrapper.like(HospitalSet::getHosname,hosname);
            }
        }

        Page<HospitalSet> hospitalSetPage = this.page(page, wrapper);
        return hospitalSetPage;
    }

    @Override
    public String getSignKey(String hoscode) {
        LambdaQueryWrapper<HospitalSet> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(HospitalSet::getHoscode, hoscode);
        HospitalSet hospitalSet = this.baseMapper.selectOne(wrapper);
        if(null == hospitalSet) {
            throw new YyghException(ResultCodeEnum.HOSPITAL_OPEN);
        }
        if(hospitalSet.getStatus()==0) {
            throw new YyghException(ResultCodeEnum.HOSPITAL_LOCK);
        }
        return hospitalSet.getSignKey();
    }
}
